window.uam = window.uam || {};
uam.Utils = {
		
	/* ************************
	 * Request Codes 
	 * ***********************/
	requestCode : {
		UP_REGISTRATION : 101,
		UP_EDIT : 102,
		UP_VIEW : 103
	},
	
	/* ************************
	 * Response Codes 
	 * ***********************/
	responseCode : {
		OK : 200,
		ERROR : 500,
		// invalid request code
		ERROR_INVALID_REQUEST_CODE : 501,
		ERROR_INVALID_PARAMETERS : 502
	},
	
	groupName : {
		MARC: "MARC",
		Privileges: "Privileges"
		
	},
	
	/**
	 * ajax call to task
	 * 
	 * @param params:Object request parameter list
	 * @param succcess:Function handle the callback
	 * 
	 */
	callTask : function(params, success){
		
		if(typeof(microstrategy) != 'undefined'){
			var session = String(microstrategy.sessionState);
		}
		
		params = params || {};
		
		params["taskId"] = params["taskId"] || 'N001';
		params["taskEnv"] = params["taskEnv"] || 'xhr';
        params["taskContentType"] = params["taskContentType"] || 'json';
        params["session"] = params["session"] || String(session);
        params["dateTime"] = params["dateTime"] || (new Date()).getTime();
        
        //formValueSubmit:
        
        success = success || function(data){ console ? console.log("callTask.data = " + data) : alert("callTask.data = " + data); }
		
        console.log(params);
        
		taskUrl = './taskProc';
		$.ajax({
			url:  taskUrl,
			type: 'GET',
			data: params,
			cache: "false",
			dataType: "text",
			async: true, //set to false in case we want it to wait
			success: success ,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
		        alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
		    } 
		});
	},
	
	/**
	 * array3D = [[ [ "OPT_BLUE_ACS_RSN_CD", 4 ], [ "OPT_BLUE_ACS_RSN_DS", {} ] ]]
	 * convert to: [[{"OPT_BLUE_ACS_RSN_CD": 4, }, {"OPT_BLUE_ACS_RSN_DS": {}}  ]]
	 */
	getObjectList : function(array3D) {
		
		if (!array3D) return;
		
		var result = [];
		for (var i = 0; i < array3D.length; i++) {
			result.push(this.getObject(array3D[i]));
		}
		return result;
	},
	
	/**
	 * array2D = [ [ "OPT_BLUE_ACS_RSN_CD", 4 ], [ "OPT_BLUE_ACS_RSN_DS", {} ]]
	 * convert to: [{"OPT_BLUE_ACS_RSN_CD": 4,}, {"OPT_BLUE_ACS_RSN_DS": {} }] 
	 */
	getObject : function(array2D) {
		
		if (!array2D) return;
		var result = {};
		for (var i = 0; i < array2D.length; i++) {
			result[array2D[i][0]] = array2D[i][1];
		}
		return result;
	},
	// MM-dd-yyyy
	date2String: function (date) {
		var year = date.getFullYear();
	    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	    var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	    return month + "-" + day + "-" + year;
	},
	// MM-dd-yyyy HH:mm:ss
	datetime2String: function (datetime){
	    var year = datetime.getFullYear();
	    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
	    var day = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
	    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
	    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
	    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
	    return month + "-" + day + "-" + year + " " + hour + ":" + minute + ":" + second;
	},
	// 
	trim : function(str, def) {
		if (!str)  {
			return def != 'undefined' ? def : null; 
		}
    	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  	}

};
uam.utils = uam.utils || uam.Utils;